'use strict'

const mediaStreamContrains = {
    video: {
        frameRate: { min: 20 },
        width: { min: 640, ideal: 1280 },
        height: { min: 360, ideal: 720 },
        aspectRatio: 16 / 9
    },
    audio: {
        echoCancelled: true,
        noiseSuppression: true,
        autoGainControl: true
    }
};
var localVideo = document.getElementById('capture');
var picture=document.getElementById("picture");
function getLocalMediaStream(mediaStream) {
    if (window.URL) {
        localVideo.srcObject = window.srcObject = mediaStream;
    } else {
        localVideo.srcObject = mediaStream;
    }
}
function handleLocalMediaStreamError(error) {
    console.log('navigator.getUserMedia error:', error);
}
navigator.mediaDevices.getUserMedia(mediaStreamContrains).then(
    getLocalMediaStream
).catch(
    handleLocalMediaStreamError
);
function takepic() {

    picture.width=640;
    picture.height=480;
    picture.getContext('2d').drawImage(localVideo,0,0,picture.width,picture.height)
}
function download(url) {
    var tmp=document.createElement("a");
    tmp.download='photo';
    tmp.href=url;
    document.body.appendChild(tmp);
    tmp.click();
    tmp.remove();
}
function savepic() {
    download(picture.toDataURL("image/jpeg"));
}